package com.bruan;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Server {
    public static void main(String[] args) {
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(8080);
            System.out.println("__________                                        _________                                                   __                 __                              \n" +
                    "\\______   \\_______ __ _______    ____            /   _____/ ______________  __ ___________            _______/  |______ ________/  |_                            \n" +
                    " |    |  _/\\_  __ \\  |  \\__  \\  /    \\   ______  \\_____  \\_/ __ \\_  __ \\  \\/ // __ \\_  __ \\  ______  /  ___/\\   __\\__  \\\\_  __ \\   __\\  ______   ______   ______ \n" +
                    " |    |   \\ |  | \\/  |  // __ \\|   |  \\ /_____/  /        \\  ___/|  | \\/\\   /\\  ___/|  | \\/ /_____/  \\___ \\  |  |  / __ \\|  | \\/|  |   /_____/  /_____/  /_____/ \n" +
                    " |______  / |__|  |____/(____  /___|  /         /_______  /\\___  >__|    \\_/  \\___  >__|            /____  > |__| (____  /__|   |__|                             \n" +
                    "        \\/                   \\/     \\/                  \\/     \\/                 \\/                     \\/            \\/  ");
            while (true) {
                Socket sock = ss.accept();
                System.out.println("connected from "+sock.getRemoteSocketAddress());
                //开启线程
                Thread t = new Handler(sock);
                t.start();
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
class Handler extends Thread{
    Socket socket;
    public Handler(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try(InputStream input = this.socket.getInputStream(); OutputStream output = this.socket.getOutputStream();
            ) {
            this.handle(input,output);
            this.socket.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("client disconnected");
    }
    private void handle(InputStream inputStream ,OutputStream outputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));

        //读取http请求
        boolean requestOk = false;
        String first = reader.readLine();
        if (first.startsWith("GET / HTTP/1.")){
            requestOk = true;
        }

        for (;;){
            String header = reader.readLine();
            if (header.isEmpty()){
                break;
            }
            System.out.println(header);
        }
        System.out.println(requestOk?"=====Response OK=====":"=====Response Error=====");

        if (!requestOk){
            writer.write("HTTP/1.0 404 NOT FOUND\r\n");
            writer.write("Content-length: 0 \r\n");

        }else {
            //成功响应
            InputStream is = Server.class.getClassLoader().getResourceAsStream("html/a.html");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder data = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null){
                data.append(line);
            }
            br.close();
            int length = data.toString().getBytes(StandardCharsets.UTF_8).length;

            writer.write("HTTP/1.1 200 OK\r\n");
            writer.write("Connection: keep-alive\r\n");
            writer.write("Content-Type:text/html\r\n");
            writer.write("Content-Length: "+length+"\r\n");
            writer.write("\r\n");
            writer.write(data.toString());
            writer.flush();

        }
    }
}
